import React from 'react';
import { Navigate, useMatch } from 'react-router-dom';

import userStore from '@/stores/user';

export interface AuthRouteProps {
  children: React.ReactNode;
}

const AuthRoute = (props: AuthRouteProps) => {
  const token = userStore(state => state.token);
  const isLoginPath = useMatch('/login');

  if (!token && !isLoginPath) {
    return <Navigate replace to="/login" />;
  }

  return <>{props.children}</>;
};

export default AuthRoute;
